import { Form } from "@b/model/base/form";
import { List } from "@b/model/base/list";
import { ListSearch } from "@b/model/base/list_search";
import type { listButton } from "@b/model/base/type";
import { pagebuttonStore } from "@b/store";
import { h, type VNode } from "vue";

export class IListSearch extends ListSearch<interDepartment> {
    constructor() {
        super()
        this.reactiveProp = []
        this.labels = {
            name: { prop: 'name', label: '名称', clearable: true, type: 'text', },
        }
    }
    init() {

    }
}

export class IList extends List<interDepartment> {
    constructor() {
        super()
        this.title = "部门列表"
        this.hasPages = false
        this.baseUrl = '/department'
        this.labels = {
            parent: { prop: 'parent.name', label: '上级科室', type: 'text' },
            name: { prop: 'name', label: '名称', type: 'text', },
            description: { prop: 'description', label: '描述', type: 'text', },
            leaderId: { prop: 'leaderId', label: '护士长', type: 'text', },
        }
        this.select = undefined
        this.editButton = () => {
            const but = pagebuttonStore().button
            const buttons: VNode[] = []
            if (but.has('base:department:add')) {
                buttons.push(h('div', { className: 'textbutton', onClick: this.add }, but.get('base:department:add')))
            }
            return buttons
        }
        this.actions = (isPhone) => {
            const actions: listButton<interDepartment>[] = []
            const but = pagebuttonStore().button
            if (but.has('base:department:update')) {
                actions.push({
                    text: but.get('base:department:update') || '',
                    type: 'primary',
                    onClick: (data) => {
                        this.edit(data.id)
                    }
                })
            }
            if (but.has('base:department:delete')) {
                actions.push({
                    text: but.get('base:department:delete') || '',
                    type: 'danger',
                    hasConfirm: true,
                    onClick: (data, index) => {
                        this.deleteItem(index)
                    }
                })
            }
            return actions
        }
    }
    async edit(_id?: number) {
        console.log(_id)
        throw new Error('未设置编辑函数')
    }
    async add(_id?: number) {
        console.log(_id)
        throw new Error('未设置新增函数')
    }
    async info(_id?: number) {
        console.log(_id)
        throw new Error('未设置详情函数')
    }
}

export class IForm extends Form<interDepartment> {
    constructor() {
        super()
        this.title = "部门详情"
        this.baseUrl = '/department'
        this.label_width = "8em"
    }
    async getSelectOptions() {
        return (await this.request('/department/select', { method: 'get' })).map((item: { name: string, id: number }) => {
            return {
                label: item.name,
                value: item.id
            }
        })
    }
    setLabels(parent: { label: string, value: number }[]) {
        this.setItem({
            name: { prop: 'name', label: '名称', clearable: true, type: 'text', },
            description: { prop: 'description', label: '描述', clearable: true, type: 'text', },
            parent: { prop: 'parent.id', label: '上级科室', type: 'select', options: parent },
            parentName: { prop: 'parentName', label: '新增上级科室', type: 'text' }
        }, this.labels)
    }
    async init() {
        const parent = await this.getSelectOptions()
        this.setLabels(parent)
    }
}
